package ar.com.spsolutions.splibrary.user;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ar.com.spsolutions.splibrary.validation.rules.ValidationRule;

public class EmailValidator extends ValidationRule {

	@Override
	protected void apply(final Object... objects) {
		final CharSequence email = (CharSequence) objects[0];
		if (email == null) {
			this.invalidate("El email no puede estar vacio");
		}
		Pattern pat = null;
		Matcher mat = null;
		pat = Pattern
				.compile("^([0-9a-zA-Z]([_.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+([a-zA-Z]{2,9}.)+[a-zA-Z]{2,3})$");
		mat = pat.matcher(email);
		if (!mat.find()) {
			this.invalidate("El email " + email + " no es valido");
		}

	}

}
